﻿@namespace Core.Web.Admin.Blazor
@using Core.Web.Admin.Blazor.Utils
@inherits LayoutComponentBase


<AuthorizeView>
    <Authorized>
        <AntDesign.ProLayout.BasicLayout Logo="@("https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg")"
                                         MenuData="_menuData">

            @*<Menu Theme=theme
                OnMenuItemClicked=handleClick
                Style=" width: 256px "
                DefaultOpenKeys=@(new []{"sub1"})
                SelectedKeys=@(new []{current})
                Mode="MenuMode.Inline">
                <SubMenu Key="sub1" TitleTemplate=@sub1Title>
                <MenuItem Key="1">Option 1</MenuItem>
                <MenuItem Key="2">Option 2</MenuItem>
                <MenuItem Key="3">Option 3</MenuItem>
                <MenuItem Key="4">Option 4</MenuItem>
                </SubMenu>
                <SubMenu Key="sub2" TitleTemplate=@sub2Title>
                <MenuItem Key="5">Option 5</MenuItem>
                <MenuItem Key="6">Option 6</MenuItem>
                <SubMenu Key="sub3" Title="Submenu">
                <MenuItem Key="7">Option 7</MenuItem>
                <MenuItem Key="8">Option 8</MenuItem>
                </SubMenu>
                </SubMenu>
                <SubMenu Key="sub4" TitleTemplate=@sub4Title>
                <MenuItem Key="9">Option 9</MenuItem>
                <MenuItem Key="10">Option 10</MenuItem>
                <MenuItem Key="11">Option 11</MenuItem>
                <MenuItem Key="12">Option 12</MenuItem>
                </SubMenu>
                </Menu>*@

            <RightContentRender>
                <Core.Web.Admin.Blazor.Components.RightContent />
            </RightContentRender>
            <ChildContent>
                @Body
            </ChildContent>
            <FooterRender>
                <FooterView Copyright="2021 Ant Design Blazor" Links="Links"></FooterView>
            </FooterRender>
        </AntDesign.ProLayout.BasicLayout>
    </Authorized>
    <NotAuthorized>
        <Result Status="warning"
                Title="您没有权限，请登录或注册用户">

            <Extra>
                <Button Type="primary" OnClick="Redirct">登录</Button>
            </Extra>
        </Result>

    </NotAuthorized>
</AuthorizeView>

<SettingDrawer />

@code
{

    private MenuDataItem[] _menuData = { };
    [Inject] public HttpClient HttpClient { get; set; }
    [Inject] public NavigationManager navigationManager { get; set; }
    [Inject] protected CurrentUserContext GetCurrentUser { get; set; }
    private CurrentUserInfo currentUser
    {
        get
        {
            return GetCurrentUser.GetCurrentUser().Result;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        if (currentUser.Id != null)
        {
            //加载权限菜单
            var menuLists = await HttpClient.GetFromJsonAsync<MenuDataItem[]>("data/menu.json");
            var _menu = new List<MenuDataItem>();
            foreach (var item in menuLists)
            {
                if (item.Authority.Count(s => s.Contains("All")) > 0)
                {
                    _menu.Add(item);
                }
                else if (item.Authority.Count(s => currentUser.Role.Contains(s)) > 0)
                {
                    _menu.Add(item);
                }
            }

            _menuData = _menu.ToArray();
        }
    }
    public void Redirct()
    {
        navigationManager.NavigateTo("Authorize/Login");
    }
    public LinkItem[] Links { get; set; }
    }
}